package com.user.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.user.dao.order.*;
import com.user.dao.restaurant.MonthOrder;
import com.user.dao.user.RestaurantOrder;
import com.user.domain.Order;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * @author YU
 * @description 针对表【order】的数据库操作Mapper
 * @createDate 2024-01-08 11:33:40
 * @Entity .domain.com.user.domain.Order
 */
public interface OrderMapper extends BaseMapper<Order> {

    /**
     * 查询餐厅座位
     *
     * @param restaurantId 餐厅Id
     * @return 时间段列表
     */
    @Select("select seat_id from `order` where date_format(date_period,'%Y-%m-%d') = #{time}  and restaurant_id = #{restaurantId}  and status != 2")
    List<Integer> getSeatIdsByTime(Integer restaurantId);

    /**
     * 查询座位时间段
     *
     * @param id         座位Id
     * @param datePeriod 时间段
     * @return 时间段列表
     */
    @Select("select time_period from `order` where seat_id = #{id}  and date_format(date_period,'%Y-%m-%d') = #{datePeriod} and status != 2")
    List<Integer> getSeatTime(Integer id, String datePeriod);

    /**
     * 查询座位时间段
     *
     * @param id         座位Id
     * @param timePeriod 时间段
     * @return 时间段列表
     */
    @Select("select count(*) from `order` where seat_id = #{id} and date_format(date_period,'%Y-%m-%d') = #{datePeriod} and time_period = #{timePeriod} and status != 2")
    Integer getSeatTimeBy(Integer id, String datePeriod, Integer timePeriod);

    /**
     * 查询用户订单
     *
     * @param order   查询条件
     * @param mapPage 分页
     * @return 订单列表
     */
    IPage<UserOrder> selectOrder(@Param("order") QueryPageOrder order, @Param("mapPage") Page<Order> mapPage);

    /**
     * 查询用户订单数量
     *
     * @param order 查询条件
     * @return 订单数量
     */
    long selectOrderCount(@Param("order") QueryPageOrder order);

    /**
     * 查询商家订单
     *
     * @param orderMerchant 查询条件
     * @param mapPage       分页
     * @return 订单列表
     */
    IPage<MerchantOrders> selectOrderByMerchant(@Param("orderMerchant") QueryPageOrderMerchant orderMerchant, @Param("mapPage") Page<Order> mapPage);

    /**
     * 查询商家订单数量
     *
     * @param orderMerchant 查询条件
     * @return 订单数量
     */
    long selectOrderCountByMerchant(@Param("orderMerchant") QueryPageOrderMerchant orderMerchant);

    /**
     * 查询订单
     *
     * @param orderId      订单Id
     * @param userId       用户Id
     * @param restaurantId 餐厅Id
     * @return 订单数量
     */
    @Select("select count(*) from `order` where id = #{orderId} and user_id = #{userId} and restaurant_id = #{restaurantId}")
    Integer selectOrderBy(Integer orderId, Integer userId, Integer restaurantId);

    @Select("select order_detail from `order` where restaurant_id = #{restaurantId} and date_format(create_time,'%Y-%m') = #{date} and (status = 1 or status = 3)")
    List<String> selectOrderByRestaurantId(Integer restaurantId, String date);

    @Select("select order_detail, date_format(create_time,'%Y-%m') as createTime from `order` where restaurant_id = #{restaurantId} and (status = 1 or status = 3)")
    List<MonthOrder> selectMonthlySalesByRestaurantId(Integer restaurantId);

    @Select("select id,create_time from `order` where status = 0 and create_time < date_sub(now(), interval 30 minute)")
    List<SchedOrder> selectUnpaidOrders();

    /**
     * 查询座位总数
     *
     * @param restaurantId 餐厅Id
     * @return 座位总数
     */
    @Select("select seat_id from `order` where restaurant_id = #{restaurantId} and status = 3")
    List<Integer> selectSeatTotal(Integer restaurantId);

    @Select("select count(*) from `order` where restaurant_id = #{restaurantId} and status = 3")
    List<MerchantOrders> selectTodayOrder(Integer restaurantId, String nowString, String tomorrowString);

    @Select("select restaurant_id, count(*) as number from `order` where user_id = #{userId} and status = 3 group by restaurant_id")
    List<RestaurantOrder> selectOrderCountByRestaurantAndUser(@Param("userId") Integer userId);
}




